package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.backend.SafetyExaminationQuestionBack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SafetyExaminationQuestionMapper;
import com.ruoyi.system.domain.SafetyExaminationQuestion;
import com.ruoyi.system.service.ISafetyExaminationQuestionService;

/**
 * 题目库Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-07
 */
@Service
public class SafetyExaminationQuestionServiceImpl extends CxBase implements ISafetyExaminationQuestionService
{
    @Autowired
    private SafetyExaminationQuestionMapper safetyExaminationQuestionMapper;

    /**
     * 查询题目库
     * 
     * @param questionId 题目库ID
     * @return 题目库
     */
    @Override
    public SafetyExaminationQuestion selectSafetyExaminationQuestionById(String questionId)
    {
        SafetyExaminationQuestion safetyExaminationQuestion = safetyExaminationQuestionMapper.selectSafetyExaminationQuestionById(questionId);
        safetyExaminationQuestion.setSetDate(StampToString(Long.valueOf(safetyExaminationQuestion.getSetDate())));
        return safetyExaminationQuestion;
    }

    /**
     * 查询题目库列表
     * 
     * @param safetyExaminationQuestionBack 题目库
     * @return 题目库
     */
    @Override
    public List<SafetyExaminationQuestion> selectSafetyExaminationQuestionList(SafetyExaminationQuestionBack safetyExaminationQuestionBack)
    {
        if(!checkNull(safetyExaminationQuestionBack.getMemo())){
            if(((safetyExaminationQuestionBack.getMemo().equals("1") || safetyExaminationQuestionBack.getMemo().equals("2"))&&!checkNull(safetyExaminationQuestionBack.getSafetyExaminationPaperId())|| safetyExaminationQuestionBack.getMemo().equals("0"))){
                startPage();
                List<SafetyExaminationQuestion> safetyExaminationQuestions = safetyExaminationQuestionMapper.selectSafetyExaminationQuestionList(safetyExaminationQuestionBack);
                for(SafetyExaminationQuestion safetyExaminationQuestion1 : safetyExaminationQuestions){
                    safetyExaminationQuestion1.setSetDate(StampToString(Long.valueOf(safetyExaminationQuestion1.getSetDate())));
                }
                return safetyExaminationQuestions;
            }
        }
        return null;
    }

    /**
     * 新增题目库
     * 
     * @param safetyExaminationQuestion 题目库
     * @return 结果
     */
    @Override
    public int insertSafetyExaminationQuestion(SafetyExaminationQuestion safetyExaminationQuestion)
    {
        if(!checkNull(safetyExaminationQuestion.getQuestionType())&&!checkNull(safetyExaminationQuestion.getQuestionAnswer())&&!checkNull(safetyExaminationQuestion.getQuestionAnswerType())
        &&!checkNull(safetyExaminationQuestion.getQuestionContent())&&!checkNull(safetyExaminationQuestion.getSetId())){
            safetyExaminationQuestion.setQuestionId(getIdStamp().toString());
            safetyExaminationQuestion.setSetDate(getStamp().toString());
            if(safetyExaminationQuestion.getQuestionAnswerType().equals("0")&&!checkNull(safetyExaminationQuestion.getSelectA())&&!checkNull(safetyExaminationQuestion.getSelectB())
            &&!checkNull(safetyExaminationQuestion.getSelectC())&&!checkNull(safetyExaminationQuestion.getSelectD())&&((safetyExaminationQuestion.getQuestionAnswer().equals("A")||safetyExaminationQuestion.getQuestionAnswer().equals("B")||safetyExaminationQuestion.getQuestionAnswer().equals("C")||safetyExaminationQuestion.getQuestionAnswer().equals("D")))){
                return safetyExaminationQuestionMapper.insertSafetyExaminationQuestion(safetyExaminationQuestion);
            }
            else if(safetyExaminationQuestion.getQuestionAnswerType().equals("1")&&(safetyExaminationQuestion.getQuestionAnswer().equals("true")||safetyExaminationQuestion.getQuestionAnswer().equals("false"))){
                return safetyExaminationQuestionMapper.insertSafetyExaminationQuestion(safetyExaminationQuestion);
            }
        }
        return 0;
    }

    /**
     * 修改题目库
     * 
     * @param safetyExaminationQuestion 题目库
     * @return 结果
     */
    @Override
    public int updateSafetyExaminationQuestion(SafetyExaminationQuestion safetyExaminationQuestion)
    {
        if(!checkNull(safetyExaminationQuestion.getQuestionId())&&!checkNull(safetyExaminationQuestion.getQuestionType())&&!checkNull(safetyExaminationQuestion.getQuestionAnswer())&&!checkNull(safetyExaminationQuestion.getQuestionAnswerType())
           &&!checkNull(safetyExaminationQuestion.getQuestionContent())&&!checkNull(safetyExaminationQuestion.getSetId())){
            safetyExaminationQuestion.setSetDate(getStamp().toString());
            if(safetyExaminationQuestion.getQuestionAnswerType().equals("0")&&!checkNull(safetyExaminationQuestion.getSelectA())&&!checkNull(safetyExaminationQuestion.getSelectB())
             &&!checkNull(safetyExaminationQuestion.getSelectC())&&!checkNull(safetyExaminationQuestion.getSelectD())&&((safetyExaminationQuestion.getQuestionAnswer().equals("A")||safetyExaminationQuestion.getQuestionAnswer().equals("B")||safetyExaminationQuestion.getQuestionAnswer().equals("C")||safetyExaminationQuestion.getQuestionAnswer().equals("D")))){
                return safetyExaminationQuestionMapper.updateSafetyExaminationQuestion(safetyExaminationQuestion);
            }
            else if(safetyExaminationQuestion.getQuestionAnswerType().equals("1")&&(safetyExaminationQuestion.getQuestionAnswer().equals("true")||safetyExaminationQuestion.getQuestionAnswer().equals("false"))){
                return safetyExaminationQuestionMapper.updateSafetyExaminationQuestion(safetyExaminationQuestion);
            }
        }
        return 0;
    }

    /**
     * 批量删除题目库
     * 
     * @param questionIds 需要删除的题目库ID
     * @return 结果
     */
    @Override
    public int deleteSafetyExaminationQuestionByIds(String[] questionIds)
    {
        return safetyExaminationQuestionMapper.deleteSafetyExaminationQuestionByIds(questionIds);
    }

    /**
     * 删除题目库信息
     * 
     * @param questionId 题目库ID
     * @return 结果
     */
    @Override
    public int deleteSafetyExaminationQuestionById(String questionId)
    {
        return safetyExaminationQuestionMapper.deleteSafetyExaminationQuestionById(questionId);
    }
}
